//var server = "http://security.jelastic.servint.net/";

var keyURL = /*server +*/ "webresources/encryption/aes/key/";
var encryptURL = /*server +*/ "webresources/encryption/aes/encrypt";
var decryptURL = /*server +*/ "webresources/encryption/aes/decrypt";

/* adding trim to String prototype*/
if (!String.prototype.trim) {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}
/***********************************/

$(function() {
  console.log("Dom ready");

  $("#btn128Key").click(function() {
    generateKey(128);
  });

  $("#btn192Key").click(function() {
    generateKey(192);
  });

  $("#btn256Key").click(function() {
    generateKey(256);
  });

  $("#btnEncrypt").click(function() {
    encrypt();
  });

  $("#btnDecrypt").click(function() {
    decrypt();
  });
});

var jqxhr = null;

function generateKey(length) {
  var url = keyURL + length;

  if (jqxhr !== null) {
    jqxhr.abort();
    $("#board").html("");
  }

  jqxhr = $.ajax({
    type: "get",
    url: url,
    contentType: "application/json",
    beforeSend: function() {
      $("#board").html("<img src='images/loader.gif' alt='Loading' />");
    }
  }).done(function(response) {
    $("#key").val(response.key);
    $("#board").html("");
  }).fail(function() {
    $("#board").html("<span class='error'>Can not connect to the server!</span>");
  }).always(function() {
    jqxhr = null;
  });
}

// encrypt

var jqxhr2 = null;

function encrypt() {
  try {
    var key = $("#key").val();

    if (key.trim().length > 0) {

      if (jqxhr2 !== null) {
        var answer = confirm("Do you want to cancel the previous request?");

        if (answer) {
          jqxhr2.abort();
          $("#board").html("");
        }

      }

      var data = $("#data").val();
      data = Base64.encode(data);

      var request = {"data": data, "key": key};

      console.log(request);

      jqxhr2 = $.ajax({
        type: "post",
        url: encryptURL,
        data: JSON.stringify(request),
        contentType: "application/json",
        beforeSend: function() {
          $("#board").html("<img src='images/loader.gif' alt='Loading' />");
        }
      }).done(function(response) {
        if (response !== null && response.status === "SUCCESS") {
          $("#cipher").val(response.data);
          $("#cipherHex").val(base64ToHex(response.data));
          $("#board").html("");
        } else {
          $("#cipher").val("");
          $("#board").html("<span class='error'>Sorry! Server error.</span>");
        }
      }).fail(function() {
        $("#board").html("<span class='error'>Can not connect to the server!</span>");
      }).always(function() {
        jqxhr2 = null;
      });

    } else {
      $("#board").html("<span class='warning'>Please enter the key!</div>");
    }
  } catch (e) {
    console.log(e.message);
    return false;
  }
}


// decrypt  

function decrypt() {
  try {
    var key = $("#key").val();

    if (key.trim().length > 0) {

      if (jqxhr2 !== null) {
        var answer = confirm("Do you want to cancel the previous request?");

        if (answer) {
          jqxhr2.abort();
          $("#board").html("");
        }

      }

      var data = $("#cipher").val();
      //data = Base64.encode(data);

      var request = {"data": data, "key": key};

      console.log(request);

      jqxhr2 = $.ajax({
        type: "post",
        url: decryptURL,
        data: JSON.stringify(request),
        contentType: "application/json",
        beforeSend: function() {
          $("#board").html("<img src='images/loader.gif' alt='Loading' />");
        }
      }).done(function(response) {
        if (response !== null && response.status === "SUCCESS") {
          $("#data").val(Base64.decode(response.data));
          $("#board").html("");
        } else {
          $("#cipher").val("");
          $("#board").html("<span class='error'>Sorry! Server error.</span>");
        }
      }).fail(function() {
        $("#board").html("<span class='error'>Can not connect to the server!</span>");
      }).always(function() {
        jqxhr2 = null;
      });

    } else {
      $("#board").html("<span class='warning'>Please enter the key!</div>");
    }
  } catch (e) {
    console.log(e.message);
    return false;
  }
}